package com.zhang.zhangapp.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.config.HttpConfig;
import com.zhang.zhangapp.bean.shop.Evaluate;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by 罗肖 on 2018/4/11.
 */

public class CommentAdapter extends BaseAdapter {
    List<Evaluate> evaluateList;
    Context context;
    ImageOptions imageOptions = new ImageOptions.Builder()
            .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
            .setRadius(DensityUtil.dip2px(2))
            .setIgnoreGif(false)
            .setCrop(true)//是否对图片进行裁剪
            .setFailureDrawableId(R.mipmap.test)
            .setLoadingDrawableId(R.mipmap.test)
            .build();

    public CommentAdapter(List<Evaluate> evaluateList, Context context) {
        this.evaluateList = evaluateList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return evaluateList == null ? 0 : evaluateList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_user_comment, null);
            x.view().inject(viewHolder, view);
            view.setTag(viewHolder);
        } else viewHolder = (ViewHolder) view.getTag();
        Evaluate evaluate = evaluateList.get(position);
        viewHolder.userName.setText(evaluate.getNickname());
        viewHolder.createDate.setText(evaluate.getCtime());
        viewHolder.commentDesc.setText(evaluate.getDescr());
        x.image().bind(viewHolder.userImage,HttpConfig.IMAGE_HTTP_PATH+evaluate.getPhoto(),imageOptions);
        if (evaluate.getPics() != null && evaluate.getPics().length > 0) {
            viewHolder.commentIamgeLayout.setVisibility(View.VISIBLE);
            viewHolder.commentImage2.setVisibility(View.INVISIBLE);
            viewHolder.commentImage3.setVisibility(View.INVISIBLE);
            viewHolder.commentImage4.setVisibility(View.INVISIBLE);
            x.image().bind(viewHolder.commentImage1, evaluate.getPics()[0], MApplication.getInstance().getImageOptions());
            if (evaluate.getPics().length > 1) {
                viewHolder.commentImage2.setVisibility(View.VISIBLE);
                x.image().bind(viewHolder.commentImage2, evaluate.getPics()[1], MApplication.getInstance().getImageOptions());
            }
            if (evaluate.getPics().length  > 2) {
                viewHolder.commentImage3.setVisibility(View.VISIBLE);
                x.image().bind(viewHolder.commentImage3, evaluate.getPics()[2], MApplication.getInstance().getImageOptions());
            }
            if (evaluate.getPics().length  > 3) {
                viewHolder.commentImage4.setVisibility(View.VISIBLE);
                x.image().bind(viewHolder.commentImage4, evaluate.getPics()[3], MApplication.getInstance().getImageOptions());
            }
        } else viewHolder.commentIamgeLayout.setVisibility(View.GONE);
        return view;
    }

    class ViewHolder {
        @ViewInject(R.id.item_comment_user_image)
        ImageView userImage;
        @ViewInject(R.id.item_comment_user_name)
        TextView userName;
        @ViewInject(R.id.item_comment_create_date)
        TextView createDate;
        @ViewInject(R.id.item_comment_iamge1)
        ImageView commentImage1;
        @ViewInject(R.id.item_comment_iamge2)
        ImageView commentImage2;
        @ViewInject(R.id.item_comment_iamge3)
        ImageView commentImage3;
        @ViewInject(R.id.item_comment_iamge4)
        ImageView commentImage4;
        @ViewInject(R.id.item_comment_image_layout)
        LinearLayout commentIamgeLayout;
        @ViewInject(R.id.item_comment_desc)
        TextView commentDesc;

    }
}
